package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysQuestionDomain;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.NaireTreeSelect;
import com.ruoyi.system.domain.SysNaire;
import com.ruoyi.system.mapper.SysNaireMapper;
import com.ruoyi.system.service.ISysQuestionDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysQuestionMapper;
import com.ruoyi.common.core.domain.entity.SysQuestion;
import com.ruoyi.system.service.ISysQuestionService;

/**
 * 问题数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-03
 */
@Service
public class SysQuestionServiceImpl implements ISysQuestionService 
{
    @Autowired
    private SysQuestionMapper sysQuestionMapper;

    @Autowired
    private ISysQuestionDomainService domainService;

    @Autowired
    private SysNaireMapper sysNaireMapper;

    /**
     * 查询问题数据
     * 
     * @param questionId 问题数据主键
     * @return 问题数据
     */
    @Override
    public SysQuestion selectSysQuestionByQuestionId(Long questionId)
    {
        return sysQuestionMapper.selectSysQuestionByQuestionId(questionId);
    }

    /**
     * 查询问题数据列表
     * 
     * @param sysQuestion 问题数据
     * @return 问题数据
     */
    @Override
    public List<SysQuestion> selectSysQuestionList(SysQuestion sysQuestion)
    {
        return sysQuestionMapper.selectSysQuestionList(sysQuestion);
    }

    @Override
    public List<SysQuestion> selectSysQuestionListWithRecords(SysQuestion sysQuestion) {
        return sysQuestionMapper.selectSysQuestionListWithRecordsByCompanyIdAndNaireId(sysQuestion);
    }

    @Override
    public  List<TreeSelect> getSurveyTreeSelect(SysQuestion sysQuestion){
        List<SysQuestion> list = sysQuestionMapper.selectSysQuestionList(sysQuestion);
        HashMap<Long, List<SysQuestion>> questions = new HashMap<>();
        for (SysQuestion question : list) {
            if(questions.containsKey(question.getDomain())){
                List<SysQuestion> tmp=questions.get(question.getDomain());
                tmp.add(tmp.size(),question);

            }else{
                List<SysQuestion> tmp=new ArrayList<>();
                tmp.add(tmp.size(),question);
                questions.put(question.getDomain(),tmp);
            }
        }
        List<SysQuestionDomain> domains = domainService.selectQuestionDomainList(new SysQuestionDomain());
        List<TreeSelect> domainTree = domainService.buildQuestionDomainTreeSelect(domains);
        return buildSurveyTreeSelect(domainTree,questions);
    }

    @Override
    public  List<NaireTreeSelect> getSurveyTreeSelectWithRecord(SysQuestion sysQuestion){
        SysNaire naire=sysNaireMapper.selectSysNaireByNaireId(sysQuestion.getNaireId());
        String[] ids=naire.getQuestions().substring(1,naire.getQuestions().length()-1).split(",");
        List<Long> gids=new ArrayList<>();

        for(int i=0;i<ids.length;i++){
            gids.add(Long.parseLong(ids[i]));
        }

        List<SysQuestion> list = sysQuestionMapper.selectSysQuestionListWithRecordsByCompanyIdAndNaireId(sysQuestion);
        HashMap<Long, List<SysQuestion>> questions = new HashMap<>();
        for (SysQuestion question : list) {
            if(!gids.contains(question.getQuestionId()))
                continue;
            if(questions.containsKey(question.getDomain())){
                List<SysQuestion> tmp=questions.get(question.getDomain());
                tmp.add(tmp.size(),question);

            }else{
                List<SysQuestion> tmp=new ArrayList<>();
                tmp.add(tmp.size(),question);
                questions.put(question.getDomain(),tmp);
            }
        }
        List<SysQuestionDomain> domains = domainService.selectQuestionDomainList(new SysQuestionDomain());
        List<TreeSelect> domainTree = domainService.buildQuestionDomainTreeSelect(domains);
        return buildNaireTreeSelect(domainTree,questions);
    }
    @Override
    public  List<TreeSelect> getSurveyTreeSelect(Set<Long> questionIds){
        List<SysQuestion> list = sysQuestionMapper.selectSysQuestionByQuestionIds(questionIds);
        HashMap<Long, List<SysQuestion>> questions = new HashMap<>();
        for (SysQuestion question : list) {
            if(questions.containsKey(question.getDomain())){
                List<SysQuestion> tmp=questions.get(question.getDomain());
                tmp.add(tmp.size(),question);

            }else{
                List<SysQuestion> tmp=new ArrayList<>();
                tmp.add(tmp.size(),question);
                questions.put(question.getDomain(),tmp);
            }
        }
        List<SysQuestionDomain> domains = domainService.selectQuestionDomainList(new SysQuestionDomain());
        List<TreeSelect> domainTree = domainService.buildQuestionDomainTreeSelect(domains);
        return buildSurveyTreeSelect(domainTree,questions);
    }

    private List<TreeSelect> buildSurveyTreeSelect(List<TreeSelect> domainTree, HashMap<Long, List<SysQuestion>> questions){

        for(TreeSelect tree : domainTree){
            if(tree.getChildren()!=null &&tree.getChildren().size()>0){
                List<TreeSelect> ntree=buildSurveyTreeSelect(tree.getChildren(),questions);
                tree.setChildren(ntree);
                tree.setId(-tree.getId());
            }else{
                if(questions.containsKey(tree.getId())){
                    List<SysQuestion> tquestions=questions.get(tree.getId());
                    List<TreeSelect> nchildren=new ArrayList<>();

                    for(SysQuestion tquestion :tquestions){
                        TreeSelect ntree=new TreeSelect(tquestion);
                        nchildren.add(nchildren.size(), ntree);

                    }
                    tree.setChildren(nchildren);
                    tree.setId(-tree.getId());
                }
            }
        }
        return domainTree;
    }


    private List<NaireTreeSelect> buildNaireTreeSelect(List<TreeSelect> domainTree, HashMap<Long, List<SysQuestion>> questions){
        List<NaireTreeSelect> strees=new ArrayList<>();
        for(TreeSelect tree : domainTree){
            NaireTreeSelect stree=new NaireTreeSelect(tree);
            if(tree.getChildren()!=null &&tree.getChildren().size()>0){
                List<NaireTreeSelect> children= buildNaireTreeSelect( tree.getChildren(),  questions);
                stree.setChildren(children);
                strees.add(strees.size(),stree);
            }else{
                if(questions.containsKey(tree.getId())){
                    stree.setQuestions(questions.get(tree.getId()));
                    strees.add(strees.size(),stree);
                }
            }
        }
        return strees;
    }
    /**
     * 新增问题数据
     * 
     * @param sysQuestion 问题数据
     * @return 结果
     */
    @Override
    public int insertSysQuestion(SysQuestion sysQuestion)
    {
        sysQuestion.setCreateTime(DateUtils.getNowDate());
        return sysQuestionMapper.insertSysQuestion(sysQuestion);
    }

    /**
     * 修改问题数据
     * 
     * @param sysQuestion 问题数据
     * @return 结果
     */
    @Override
    public int updateSysQuestion(SysQuestion sysQuestion)
    {
        sysQuestion.setUpdateTime(DateUtils.getNowDate());
        return sysQuestionMapper.updateSysQuestion(sysQuestion);
    }

    /**
     * 批量删除问题数据
     * 
     * @param questionIds 需要删除的问题数据主键
     * @return 结果
     */
    @Override
    public int deleteSysQuestionByQuestionIds(Long[] questionIds)
    {
        return sysQuestionMapper.deleteSysQuestionByQuestionIds(questionIds);
    }

    /**
     * 删除问题数据信息
     * 
     * @param questionId 问题数据主键
     * @return 结果
     */
    @Override
    public int deleteSysQuestionByQuestionId(Long questionId)
    {
        return sysQuestionMapper.deleteSysQuestionByQuestionId(questionId);
    }

    @Override
    public List<SysQuestion> selectSysQuestionByQuestionIds(Set<Long> questionIds) {
        return sysQuestionMapper.selectSysQuestionByQuestionIds(questionIds);
    }
}
